<?php

namespace app\admin\controller;

use app\common\model\Leibie;
use app\common\model\Renwuleixing;
use app\common\traits\Page;
use function PHPSTORM_META\type;
use think\Db;
use app\common\model\Renwus;

class Renwu extends Admin
{
    use Page;
    /**
     * 任务显示
     * @return mixed
     */
    public function index()
    {
//        $renwu = new Renwus();
        $info = $this->pageshow('renwus',['status'=>1,'is_total'=>1]);
        foreach ($info as $k=>$v){
            $arr = Db::name('renwus')->where(['is_total'=>2,'rwlx_id'=>$v['rwlx_id'],'date'=>$v['date']])->select();
            if($arr){
                $info[$k]['next'] = 1;
            }else{
                $info[$k]['next'] = 0;
            }
        }
//        var_dump($info);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 新增任务
     * @return mixed
     */
    public function add(){
        if($this->request->isPost()){
            $renwu = new Renwus();
            $data = $this->request->param();
            $arr['date'] = array_shift($data);
            $re = $this->validate($arr,'Renwus.date');
            if( $re !== true ){
                $this->error($re);
            }
            $info = Db::name('renwus')->where(['date'=>$arr['date'],'is_total'=>1])->find();
            if($info){
                $this->error('此月份任务已经存在');
            }
            foreach ($data as $v){
                list($arr['rwlx_id'],$arr['renwu'])=$v;
                $res = $this->validate(
                    ['renwu'=>$arr['renwu']],
                    ['renwu'=>'require|regex:[1-9]\d*'],
                    ['renwu.require'=>'任务必填','renwu.regex'=>'任务必须为正整数']
                );
                if( $res !== true ){
                    $this->error($res);
                }
                $arr['create_time'] = time();
                $arr['czry'] = session('admin_user.adminid');
                $arr['is_total'] = 1;//总任务
                $arrs[]=$arr;
            }
            $result = $renwu->saveAll($arrs);
            if($result){
                $this->success('新增任务成功','index');
            }else{
                $this->error($this->getError());
            }
        }else{
            $this->getXgsj();
            return $this->fetch();
        }
    }

    public function editAll(){
        $renwu = new Renwus();
        $data = $this->request->param();
        if($this->request->isPost()){
            foreach ($data as $v){
                list($arr['id'],$arr['renwu'])=$v;
                $res = $this->validate(
                    ['renwu'=>$arr['renwu']],
                    ['renwu'=>'require|regex:[1-9]\d*'],
                    ['renwu.require'=>'任务必填','renwu.regex'=>'任务必须为正整数']
                );
                if( $res !== true ){
                    $this->error($res);
                }
                $arr['create_time'] = time();
                $arr['czry'] = session('admin_user.adminid');
                $arrs[]=$arr;
            }
            $result = $renwu->saveAll($arrs);
            if($result){
                $this->success('修改任务成功','index');
            }else{
                $this->error($this->getError());
            }
        }else{
            $info = $renwu->where(['date'=>$data['date'],'is_total'=>1,'status'=>1])->select();
            $this->assign('date',$data['date']);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //新增邀约任务
    public function add_Yaoyue(){
        $renwu = new Renwus();
        $data = $this->request->param();
        if($this->request->isPost()){
            $arr['rwlx_id'] = array_shift($data);
            $arr['date'] = array_shift($data);
            $re = $this->validate($arr,'Renwus.date');
            if( $re !== true ){
                $this->error($re);
            }
            $info = Db::name('renwus')->where(['date'=>$arr['date'],'is_total'=>2,'rwlx_id'=>$arr['rwlx_id']])->find();//邀约任务
            if($info){
                $this->error('此月份该类邀约任务已经存在');
            }
            foreach ($data as $v){
                list($arr['ygroup_id'],$arr['renwu'])=$v;
                $res = $this->validate(
                    ['renwu'=>$arr['renwu']],
                    ['renwu'=>'require|regex:[1-9]\d*'],
                    ['renwu.require'=>'任务必填','renwu.regex'=>'任务必须为正整数']
                );
                if( $res !== true ){
                    $this->error($res);
                }
                $arr['create_time'] = time();
                $arr['czry'] = session('admin_user.adminid');
                $arr['is_total'] = 2;//子任务
//                $arr['rwlx_id'] = 2;//邀约任务
                $arr['depar_id'] = 3;//邀约部
                $arrs[]=$arr;
            }
            $result = $renwu->saveAll($arrs);
            if($result){
                $this->success('新增邀约任务成功','index');
            }else{
                $this->error($this->getError());
            }
        }else{
            $this->assign('data',$data);//传类型和月份
            $rwinfo = Db::name('renwuleixing')->where(['status'=>1,'id'=>$data['rwlx_id']])->field('name')->find();
            $this->assign('rwname',$rwinfo['name']);

            $ygroup = Db::name('ygroup')->where(['status'=>1,'depar_id'=>3])->field('id,group_name')->select();
            $this->assign('ygroup',$ygroup);
            return $this->fetch('add-yaoyue');
        }
    }

    //编辑邀约任务
    public function edit_Yaoyue(){
        $renwu = new Renwus();
        $data = $this->request->param();
        if($this->request->isPost()){
            foreach ($data as $v){
                list($arr['id'],$arr['renwu'])=$v;
                $res = $this->validate(
                    ['renwu'=>$arr['renwu']],
                    ['renwu'=>'require|regex:[1-9]\d*'],
                    ['renwu.require'=>'任务必填','renwu.regex'=>'任务必须为正整数']
                );
                if( $res !== true ){
                    $this->error($res);
                }
                $arr['create_time'] = time();
                $arr['czry'] = session('admin_user.adminid');
                $arrs[]=$arr;
            }
            $result = $renwu->saveAll($arrs);
            if($result){
                $this->success('修改邀约任务成功','index');
            }else{
                $this->error($this->getError());
            }
        }else{
            $info = $renwu->where(['date'=>$data['date'],'rwlx_id'=>$data['rwlx_id'],'is_total'=>2,'status'=>1])->select();
            if( !empty($info->toArray()) ){
                $rwinfo = Db::name('renwuleixing')->where(['status'=>1,'id'=>$data['rwlx_id']])->field('name')->find();
                $this->assign('rwname',$rwinfo['name']);
                $this->assign('date',$data['date']);
                $this->assign('info',$info);
                return $this->fetch('edit-yaoyue');
            }else{
                $this->error('还未录入此月邀约任务，请先录入');
            }
        }
    }

    /**
     * 编辑任务
     * @return mixed
     */
//    public function edit()
//    {
//        $renwu = new Renwus();
//        $data = $this->request->param();
//        if($this->request->isPost()){
//
//            !empty($data['status']) ?: $data['status'] = 2;
//            $data['create_time'] = time();
//            $data['czry'] = session('admin_user.adminid');
//            $res = $renwu->isUpdate(true)->save($data);
//            if($res){
//                return ['status'=>'1','info'=>"修改任务成功",'url'=>url('index')];
//            }else{//失败
//                return json(['status'=>'0','info'=>$this->getError()]);
//            }
//        }else{
//            $this->getXgsj();
////            $info = Db::name('renwus')->where(['id'=>$data['id'],'status'=>1])->find();
//            $info = $renwu->where(['id'=>$data['id'],'status'=>1])->find();
//            $this->assign('info',$info);
//            return $this->fetch();
//        }
//    }


    public function getXgsj(){
        $renwu = new Renwus();
        $leibie = new Leibie();
        $renwuleixing = $renwu->getRenwuleixing();
        $depar = $leibie->getBumen();
        $ygroup    =  $renwu->getYgroup();

        $this->assign('ygroup',$ygroup);
        $this->assign('renwuleixing',$renwuleixing);
        $this->assign('depar',$depar);
    }

    //任务类型管理
    public function renwuleixing(){
        $renwuleixing = new Renwuleixing();
        $info = $renwuleixing->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //新增任务类型
    public function rwlx_Add(){
        if($this->request->isPost()){
            $renwuleixing = new Renwuleixing();
            $data = $this->request->param();
            $data['create_time'] = time();
            $data['czry'] = session('admin_user.adminid');
            $res = $renwuleixing->save($data);
            if($res){
                return ['status'=>'1','info'=>"新增类型成功",'url'=>url('rwlx_Add')];
            }else{//失败
                return json(['status'=>'0','info'=>$this->getError()]);
            }
        }else{
            return $this->fetch();
        }
    }
    //编辑任务类型
    public function rwlx_Edit(){
        $data = $this->request->param();
        if($this->request->isPost()){
            $renwuleixing = new Renwuleixing();
            !empty($data['status']) ?: $data['status'] = 2;
            $data['create_time'] = time();
            $data['czry'] = session('admin_user.adminid');
            $res = $renwuleixing->isUpdate(true)->save($data);
            if($res){
                return ['status'=>'1','info'=>"修改类型成功",'url'=>url('renwuleixing')];
            }else{//失败
                return json(['status'=>'0','info'=>$this->getError()]);
            }
        }else{
            $info = Db::name('renwuleixing')->where(['id'=>$data['id']])->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

}
